/*
** Copyright (C) 2015-2016  Michael Roland <mi.roland@fh.hagenberg.at>
**                          FH OÖ Forschungs & Entwicklungs GmbH
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
*/
package org.simalliance.openmobileapi.service.terminals.exploit.activities;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import org.simalliance.openmobileapi.service.terminals.exploit.R;

public class ViewerActivity extends Activity {
    private static final String LOG_TAG = ViewerActivity.class.getSimpleName();
    
    public static final String EXTRA_VIEWDATA = "view_data";
    public static final String EXTRA_VIEWDATANAME = "view_data_name";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewer);

        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        
        Intent intent = getIntent();
        String viewData = intent.getStringExtra(EXTRA_VIEWDATA);
        
        if (viewData != null) {
            TextView view = (TextView)findViewById(R.id.main_view);
            view.setText(viewData);
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.viewer, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
                
            case R.id.menu_about:
                this.startActivity(new Intent(this, AboutActivity.class));
                break;
                
            case R.id.menu_save:
                {
                    Intent intent = getIntent();
                    String viewData = intent.getStringExtra(EXTRA_VIEWDATA);
                    String viewDataName = intent.getStringExtra(EXTRA_VIEWDATANAME);

                    if (viewDataName == null) {
                        viewDataName = "";
                    }
                    
                    if (viewData != null) {
                        File path = getExternalFilesDir(null);
                        File file = new File(path, "data-" + viewDataName + "-" + System.currentTimeMillis());
                        try {
                            FileOutputStream fileOutStream = new FileOutputStream(file);
                            PrintStream printStream = new PrintStream(fileOutStream, false, "UTF-8");
                            printStream.print(viewData);
                            printStream.flush();
                            printStream.close();
                            fileOutStream.flush();
                            fileOutStream.close();
                            MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, null);

                            Toast toast = Toast.makeText(this, getText(R.string.saved_to_file) + " " + file.getAbsolutePath(), Toast.LENGTH_SHORT);
                            toast.show();
                        } catch (Exception e) {
                            Log.e(LOG_TAG, "Could not process data", e);
                            Toast toast = Toast.makeText(this, R.string.write_failed, Toast.LENGTH_SHORT);
                            toast.show();
                        }
                    }
                }
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}
